package com.jinkaimen.kqb.ui.activity.addmerchant;

import android.graphics.Color;
import android.os.Message;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.manager.ApiManager;
import com.jinkaimen.kqb.utils.ValidateUtils;

import retrofit2.Call;
import retrofit2.Response;

public class ResetPwdActivity extends BaseActivity {

    private EditText mTelView;
    private TextView mGetValidateBtn;
    private EditText mValidateView;
    private EditText mPwdView;
    private CheckBox mSeePwdView;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("找回密码");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_reset_pwd;
    }

    @Override
    protected void initChildView() {
        mTelView = (EditText) findViewById(R.id.et_activity_register_tel);
        mGetValidateBtn = (TextView) findViewById(R.id.tv_activity_register_get_validate);

        mValidateView = (EditText) findViewById(R.id.et_activity_register_validate);
        mPwdView = (EditText) findViewById(R.id.et_activity_register_pwd);

        mSeePwdView = (CheckBox) findViewById(R.id.cb_activity_register_see_pwd);
    }

    @Override
    protected void initChildData() {

    }

    @Override
    protected void setChildViewListener() {
        mSeePwdView.setOnClickListener(this);
        mGetValidateBtn.setOnClickListener(this);
        findViewById(R.id.btn_activity_register).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        String tel = mTelView.getText().toString();
        String code = mValidateView.getText().toString();
        String pwd = mPwdView.getText().toString();

        switch (v.getId()){
            case R.id.tv_activity_register_get_validate:
//                if (!ValidateUtils.isTelValidate(tel)) return;
//
//                mGetValidateBtn.setEnabled(false);
//                showLoadingDialogCanotCancle();
//                ApiManager.getApi().sendValidate(`
//                        "HSY001003",
//                        tel,
//                        "2"
//                ).enqueue(new NetCallBack<String>() {
//                    @Override
//                    protected void onSuccess(Call<String> call, Response<String> response) {
//                        new ValidateThread(mHandler).start();
//                        mGetValidateBtn.setTextColor(Color.parseColor("#bebebe"));
//                    }
//
//                    @Override
//                    protected void hiddenProgress() {
//                        hideLodingDialog();
//                    }
//                });
                break;
            case R.id.btn_activity_register:
                if (!ValidateUtils.isPwdValidate(pwd)) return;

                showLoadingDialog();
                ApiManager.getApi().resetPwd(
                        "HSY001004",
                        tel,
                        pwd,
                        code
                ).enqueue(new NetCallBack<String>() {
                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        UIUtils.showToast("重置密码成功");
                        ActivityUtils.finishActivity(ResetPwdActivity.this);
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }

                    @Override
                    protected boolean isNullNeedCallback() {
                        return true;
                    }
                });
                break;
            case R.id.cb_activity_register_see_pwd:
                if (mSeePwdView.isChecked()){
                    mPwdView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mPwdView.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
        }
    }

    @Override
    protected void handleMessage(BaseActivity context, Message msg) {
        switch (msg.what){
            case 100:
                mGetValidateBtn.setText(msg.obj.toString());
                break;
            case 200:
                mGetValidateBtn.setTextColor(Color.parseColor("#6284dd"));
                mGetValidateBtn.setText("获取验证码");
                mGetValidateBtn.setEnabled(true);
                break;
        }
    }
}
